package com.stxx.stiot2.controller;

import com.stxx.framwork.domain.R;
import com.stxx.framwork.domain.model.Option;
import com.stxx.framwork.domain.model.PageQuery;
import com.stxx.framwork.domain.model.PageResult;
import com.stxx.stiot2.domain.Device;
import com.stxx.stiot2.domain.Terminal;
import com.stxx.stiot2.domain.ext.DeviceSignal;
import com.stxx.stiot2.service.DeviceService;
import com.stxx.stiot2.service.SysConfigService;
import org.noear.solon.annotation.*;

import java.sql.SQLException;
import java.util.List;

@Controller
@Mapping("/device")
public class DeviceController {

    @Inject
    DeviceService baseService;
    @Inject
    SysConfigService configService;

    @Get
    @Mapping("/list")
    public PageResult<Device> list(Device bo, PageQuery pageQuery) throws SQLException {
        PageResult<Device> r = baseService.pageList(bo,pageQuery);
        return r;
    }

    @Get
    @Mapping("/{deviceId}/signals")
    public R<List<DeviceSignal>> signals(@Path Long deviceId) throws SQLException {
        List<DeviceSignal> r = baseService.getSignals(deviceId);
        return R.ok(r);
    }

    @Post
    @Mapping("")
    public R<Object> add(@Body Device bo) throws SQLException {
        baseService.insert(bo);
        return R.ok("数据保存成功");
    }

    @Put
    @Mapping("")
    public R<Object> edit(@Body Device bo) throws SQLException {
        baseService.update(bo);
        return R.ok("数据保存成功");
    }

    @Delete
    @Mapping("/{ids}")
    public R<Object> remove(@Path Long[] ids) throws SQLException {
        baseService.deleteByIds(ids);
        return R.ok("数据删除成功");
    }

    @Get
    @Mapping("/asset-type")
    public R<Object> assetType() throws SQLException {
        List<Option> list = configService.options(SysConfigService.Keys.asset_type);
        return R.ok(list);
    }
}
